package cn.doways.api.utils;

import com.baidu.aip.http.AipHttpClient;
import com.baidu.aip.http.AipRequest;
import com.baidu.aip.http.AipResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName BAipFace
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-24 22:28
 **/
public class ToolsAipFace {

    public static String getAccessToken(){
        AipRequest aipRequest=new AipRequest();
        aipRequest.setUri("https://aip.baidubce.com/oauth/2.0/token");
        HashMap<String,String> map= new HashMap<>(3);
        map.put("grant_type","client_credentials");
        map.put("client_id","MoGP6MW0xvvTLZg6sgsKYb1V");
        map.put("client_secret","Z6ePHVoGQcmb4vGhZKzErfbQ5i2O30Af");
        aipRequest.setParams(map);
        AipResponse aipResponse=AipHttpClient.post(aipRequest);
        ObjectMapper objectMapper =new ObjectMapper();
        try {
            Map json=objectMapper.readValue(aipResponse.getBodyStr(),Map.class);
            return (String) json.get("access_token");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
